package cn.com.nes.site.service.inventory.impl;

import cn.com.nes.common.annotation.OpeTransactional;
import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.common.util.DBUtils;
import cn.com.nes.mybatis.agent.inventory.entity.GOrderIn;
import cn.com.nes.mybatis.agent.inventory.entity.GOrderInApply;
import cn.com.nes.mybatis.agent.inventory.mapper.GOrderInApplyMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.entity.vo.inventory.DelApplyInVO;
import cn.com.nes.site.entity.vo.inventory.SaveApplyInfoGoodsVO;
import cn.com.nes.site.entity.vo.inventory.SaveApplyInfoInVO;
import cn.com.nes.site.entity.vo.inventory.UpdateApplyGoodsInfoInVO;
import cn.com.nes.site.module.dao.inventory.OrderInDaoImpl;
import cn.com.nes.site.service.inventory.GOrderInApplyService;
import cn.com.nes.site.service.inventory.GOrderInService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class GOrderInApplyServiceImpl extends ServiceImpl<GOrderInApplyMapper, GOrderInApply> implements GOrderInApplyService {
    @Resource
    private GOrderInService gOrderInService;

    @Resource
    private OrderInDaoImpl orderInDaoImpl;

    @Override
    @OpeTransactional
    public Integer saveApplyInfo(SaveApplyInfoInVO vo) throws SQLException {
        Integer res = 0;
        String orderState = vo.getOrderState();
        if (StrUtil.isEmpty(orderState)){
            orderState = "01";
        }
        GOrderInApply orderInApply = new GOrderInApply();
        orderInApply.setPurchaseId(vo.getPurchaseId());
        orderInApply.setOrderType(vo.getOrderType());
        orderInApply.setApplyTime(vo.getApplyTime());

        orderInApply.setCustomerId(vo.getCustomerID());
        orderInApply.setAddress(vo.getAddress());
        orderInApply.setPhone(vo.getPhone());
        orderInApply.setCusname(vo.getCusname());
        if (StrUtil.isEmpty(vo.getApplyId())) {
            String applyId = DBUtils.getDjbh(vo.getOperateUserCompanyId(), "SQD");
            orderInApply.setApplyId(applyId);
            orderInApply.setOrderState(orderState);
            orderInApply.setDepartmentId(vo.getUserInfo().getDepartmentId());
            orderInApply.setApplyUid(vo.getOperateUserId());
            orderInApply.setCompanyId(vo.getOperateUserCompanyId());
            orderInApply.setIsdelete(DeleteEnum._0.getKey());
            List<SaveApplyInfoGoodsVO> goodslist = vo.getGoodslist();
            if (!Collections.isEmpty(goodslist)){
                for (SaveApplyInfoGoodsVO saveApplyInfoGoodsVO : goodslist) {
                    //设置序号
                    Integer serial = orderInDaoImpl.getApplyNewSerial(applyId);
                    if(serial!=null) {
                        orderInApply.setSerial(serial+1);
                    }else {
                        orderInApply.setSerial(1);
                    }
                    orderInApply.setUuid(UUID.randomUUID().toString());
                    orderInApply.setGoodsUUid(saveApplyInfoGoodsVO.getGoodsUUid());
                    orderInApply.setInoutNum(new BigDecimal(saveApplyInfoGoodsVO.getInoutNum()));
                    orderInApply.setMemo(saveApplyInfoGoodsVO.getMemo());
                    res = this.baseMapper.insert(orderInApply);
                }
            }
        } else {
            LambdaQueryWrapper<GOrderInApply> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GOrderInApply::getApplyId,vo.getApplyId());
            res = this.baseMapper.update(orderInApply, wrapper);
        }
        return res;
    }

    @Override
    @OpeTransactional
    public Integer updateApplyGoodsInfo(UpdateApplyGoodsInfoInVO vo) {
        GOrderInApply gOrderInApply = new GOrderInApply();
        gOrderInApply.setUuid(vo.getUuid());
        gOrderInApply.setInoutNum(vo.getInoutNum());
        gOrderInApply.setMemo(vo.getMemo());
        return this.baseMapper.updateById(gOrderInApply);
    }

    @Override
    @OpeTransactional
    public Integer delApply(DelApplyInVO vo) {
        Integer res = 1;
        GOrderInApply orderInApply = new GOrderInApply();
        orderInApply.setIsdelete(DeleteEnum._1.getKey());
        LambdaQueryWrapper<GOrderInApply> wrapper = new LambdaQueryWrapper<>();
        //删除申请单
        if (StrUtil.isNotEmpty(vo.getApplyId())){
            String applyId = vo.getApplyId();
            wrapper.eq(GOrderInApply::getApplyId,applyId);
            res = this.baseMapper.update(orderInApply, wrapper);
            //删除关联申请单的入库单
            LambdaQueryWrapper<GOrderIn> gOrderInWrapper = new LambdaQueryWrapper<>();
            gOrderInWrapper.eq(GOrderIn::getApplyId, applyId);
            GOrderIn gOrderIn = new GOrderIn();
            gOrderIn.setIsdelete(DeleteEnum._1.getKey());
            gOrderInService.updateByWrapper(gOrderIn,gOrderInWrapper);
        } else if (StrUtil.isNotEmpty(vo.getUuid())){//删除商品明细
            String uuid = vo.getUuid();
            orderInApply.setUuid(uuid);
            res = this.baseMapper.updateById(orderInApply);
            //如果删除的serial为1的话 把所有其他的serial减1
            GOrderInApply gOrderInApply = this.baseMapper.selectById(uuid);
            if (gOrderInApply.getSerial() == 1){
                //更新serial-1
                wrapper.eq(GOrderInApply::getApplyId,gOrderInApply.getApplyId());
                orderInApply.setSerial(gOrderInApply.getSerial() - 1);
                orderInApply.setIsdelete(DeleteEnum._0.getKey());
                this.baseMapper.update(orderInApply, wrapper);
            }
        } else {
            throw new BusinessException("删除失败");
        }
        return res;
    }

}
